using System;
using System.Collections.Generic;

namespace TestCases.Utils.Comparers
{
    public class EnumerableComparer<T>: IComparer<IEnumerable<T>>
    {
        private readonly IComparer<T> _itemComparer;

        public EnumerableComparer()
        {            
        }
        
        public EnumerableComparer(IComparer<T> itemComparer)
        {
            _itemComparer = itemComparer;
        }

        public int Compare(IEnumerable<T> x, IEnumerable<T> y)
        {
            var enumerator1 = x.GetEnumerator();
            var enumerator2 = y.GetEnumerator();
            while (enumerator1.MoveNext() && enumerator2.MoveNext())
            {
                var result = (_itemComparer != null)
                    ? _itemComparer.Compare(enumerator1.Current, enumerator2.Current)
                    : ((IComparable<T>) enumerator1.Current).CompareTo(enumerator2.Current);
                                    
                if (result != 0)
                {
                    return result;
                }                
            }
            return 0;            
        }
    }
}